package com.sduept.nwld.dataserver.controller.daily;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import com.sduept.bigdata.fault.facde.FaultGenerateFacde;
import com.sduept.nwld.dataserver.manager.protection.RunlogManager;
import com.sduept.nwld.dataserver.model.daily.Runlog;
import com.sduept.utils.DateUtil;

@Named
@ViewScoped
public class RunlogController extends DailyReportAbstractController {

	private static final long serialVersionUID = 797974837070036813L;

	@Autowired
	private RunlogManager run;
	@Autowired
	private FaultGenerateFacde facde;

	private List<Runlog> log = new ArrayList<>();
	private List<String> allTypes = new ArrayList<>();
	private Runlog selectLog = null;
	private Runlog created = null;
	private String searchLike = "";
	private String selectedType = "";

	@PostConstruct
	private void init() {
		allTypes = run.getRunlogTypes();
		log = run.getRunlogByTime(queryDate1, queryDate2);
	}

	public void resetQuery() {
		searchLike = "";
		selectedType = "";
		queryDate1 = null;
		queryDate2 = null;
		resetqueryDate();
		queryByTime();
		selectLog = null;
	}

	/**
	 * 获取调度日志类型
	 * 
	 * @param r
	 * @return 类型代码
	 */
	public String getRowClass(Runlog r) {
		String type = r.getLog_type();
		switch (type) {
		case "中调汇报":
			return "zdhb";
		case "其他异常":
			return "qtyc";
		case "电网故障":
			return "dwgz";
		case "综合停电":
			return "zhtd";
		default:
			return "";
		}
	}

	/**
	 * 修改调度日志
	 */
	public void update() {
		if (selectLog != null) {
			run.editUpdate(selectLog);
			addSuccessMessage("保存成功");
		}
	}

	/**
	 * 新建按钮触发事件
	 */
	public void creatRunlog() {
		created = new Runlog();
	}

	/**
	 * opendialog保存按钮触发事件
	 */
	public void saveNew() {
		if (created != null) {
			created = run.creatRunlog(created);
			log.add(0, created);
		}
		created = null;
		addSuccessMessage("保存成功");
	}

	/**
	 * 调度日志删除方法
	 */
	public void deleteLog() {
		if (selectLog != null) {
			run.delRunlog(selectLog);
			log.remove(selectLog);
			selectLog = null;
		} else {
			addErrorMessage("请选择一条数据进行删除");
		}
		addSuccessMessage("删除成功！");
	}

	public void RowSelect(SelectEvent event) {
		selectLog = (Runlog) event.getObject();
	}

	/**
	 * 页面查询方法
	 */
	public void queryByTime() {
		if (queryDate1 == null) {
			queryDate1 = new Date(0);
		}
		if (queryDate2 == null) {
			queryDate2 = new Date();
		}
		queryDate2 = DateUtil.addDays(queryDate2, 1);
		queryDate2 = DateUtil.addSeconds(queryDate2, -1);
		log = run.getRunlogByCondition(queryDate1, queryDate2, searchLike, selectedType);
		refreshQueryParamter();
		selectLog = null;
		created = null;
	}

	public String replaceWithBr(String str) {
		str = str.replaceAll("(\r\n|\r|\n|\n\r)", "<br/>");
		return str;
	}

	/**
	 * 导入文件dialog
	 */
	public void getEquipmentByStation() {
		Map<String, Object> options = new HashMap<String, Object>();
		options.put("resizable", true);
		options.put("draggable", true);
		// options.put("responsive", true);
		options.put("modal", false);
		options.put("width", 600);
		options.put("height", 540);
		Map<String, List<String>> params = new HashMap<>();
		List<String> s = new ArrayList<>();
		s.add("t_daily_runlog");
		params.put("tName", s);
		openDialog("/ui/fileimportdb/dialog/fileUploadDialog",
				params, options);
	}

	public void reGenerateFault() {
		//ArrayList<Runlog> list = new ArrayList<Runlog>();
		//list.add(selectLog);
		//runlog2FaultManager.analyzeToFaultRecord(list);
		facde.generateFaultByRunlog(selectLog.getId(), selectLog.getLog_contents(), selectLog.getTime());
		addSuccessMessage("故障重新生成成功！");
	}

	public List<Runlog> getLog() {
		return log;
	}

	public void setLog(List<Runlog> log) {
		this.log = log;
	}

	public Runlog getSelectLog() {
		return selectLog;
	}

	public void setSelectLog(Runlog selectLog) {
		this.selectLog = selectLog;
	}

	public Runlog getCreated() {
		return created;
	}

	public void setCreated(Runlog created) {
		this.created = created;
	}

	public String getSearchLike() {
		return searchLike;
	}

	public void setSearchLike(String searchLike) {
		this.searchLike = searchLike;
	}

	public List<String> getAllTypes() {
		return allTypes;
	}

	public void setAllTypes(List<String> allTypes) {
		this.allTypes = allTypes;
	}

	public String getSelectedType() {
		return selectedType;
	}

	public void setSelectedType(String selectedType) {
		this.selectedType = selectedType;
	}
}
